<html xmlns:th="http://www.thymeleaf.org">

<div class="easyui-layout" data-options="fit:true,border:false">
    <div data-options="region:'center',border:false">
        <table id="resourceDataGrid" data-options="fit:true,border:false"></table>
    </div>
</div>
<div id="resourceToolbar" style="display: none;">
    <div th:if="${@sec.hasPermission('resource:add')}">
        <a onclick="resourceAddFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'glyphicon-plus icon-green'">添加</a>
    </div>
</div>

<script type="text/javascript" th:inline="javascript">
    var resourceDataGrid;
    $(function() {
        resourceDataGrid = $('#resourceDataGrid').treegrid({
            url : basePath + '/resource/dataGrid',
            idField : 'id',
            treeField : 'name',
            parentField : 'pid',
            fit : true,
            fitColumns : false,
            border : false,
            frozenColumns : [ [ {
                title : '编号',
                field : 'id',
                width : 40
            } ] ],
            columns : [ [ {
                field : 'name',
                title : '资源名称',
                width : 150
            }, {
                field : 'url',
                title : '资源路径',
                width : 180
            }, {
                field : 'openMode',
                title : '打开方式',
                width : 60
            }, {
                field : 'opened',
                title : '菜单状态',
                width : 60,
                formatter : function(value, row, index) {
                    return top.columnsDictFormat("resourceOpened", value);
                }
            }, {
                field : 'seq',
                title : '排序',
                width : 40
            }, {
                field : 'iconCls',
                title : '图标',
                width : 120
            }, {
                width : 120,
                title : '权限表达式',
                field : 'permissions'
            }, {
                field : 'resourceType',
                title : '资源类型',
                width : 80,
                formatter : function(value, row, index) {
                    return top.columnsDictFormat("resourceType", value);
                }
            }, {
                field : 'pid',
                title : '上级资源ID',
                width : 150,
                hidden : true
            }, {
                field : 'action',
                title : '操作',
                width : 200,
                formatter : function(value, row, index) {
                    var str = '';
                    /*[# th:if="${@sec.hasPermission('resource:edit')}"]*/
                    str += $.formatString('<a href="javascript:void(0)" class="resource-easyui-linkbutton-edit" data-options="plain:true,iconCls:\'glyphicon-pencil icon-blue\'" onclick="resourceEditFun(\'{0}\');" >编辑</a>', row.id);
                    /*[/]*/
                    /*[# th:if="${@sec.hasPermission('resource:delete')}"]*/
                    str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
                    str += $.formatString('<a href="javascript:void(0)" class="resource-easyui-linkbutton-del" data-options="plain:true,iconCls:\'glyphicon-trash icon-red\'" onclick="resourceDeleteFun(\'{0}\');" >删除</a>', row.id);
                    /*[/]*/
                    return str;
                }
            } ] ],
            onLoadSuccess:function(data){
                $('.resource-easyui-linkbutton-edit').linkbutton({text:'编辑'});
                $('.resource-easyui-linkbutton-del').linkbutton({text:'删除'});
            },
            toolbar : '#resourceToolbar'
        });
    });

    /**
     * 添加框
     */
    function resourceAddFun() {
        var rows = resourceDataGrid.treegrid('getSelected');
        var url = basePath + '/resource/addPage';
        // 如果选定的为菜单，则自动选择 新建的上级菜单
        if (rows && rows.resourceType === 0) {
            url += '?pid=' + rows.id;
        }
        parent.$.modalDialog({
            title : '添加',
            width : 580,
            height : 350,
            href : url,
            buttons : [ {
                text : '确定',
                handler : function() {
                    parent.$.modalDialog.openner_dataGrid = resourceDataGrid;
                    var f = parent.$.modalDialog.handler.find('#resourceAddForm');
                    f.submit();
                }
            } ]
        });
    }

    /**
     * 编辑
     */
    function resourceEditFun(id) {
        if (id === undefined) {
            var rows = resourceDataGrid.treegrid('getSelected');
            id = rows.id;
        } else {
            resourceDataGrid.treegrid('select', id);
        }
        parent.$.modalDialog({
            title : '编辑',
            width : 580,
            height : 350,
            href : basePath + '/resource/editPage?id=' + id,
            buttons : [ {
                text : '确定',
                handler : function() {
                    parent.$.modalDialog.openner_dataGrid = resourceDataGrid;
                    var f = parent.$.modalDialog.handler.find('#resourceEditForm');
                    f.submit();
                }
            } ]
        });
    }

    /**
     * 删除
     */
    function resourceDeleteFun(id) {
        if (id === undefined) {//点击右键菜单才会触发这个
            var rows = resourceDataGrid.treegrid('getSelected');
            id = rows.id;
        } else {//点击操作里面的删除图标会触发这个
            resourceDataGrid.treegrid('select', id);
        }
        parent.$.messager.confirm('询问', '您是否要删除当前资源？', function(b) {
            if (b) {
                progressLoad();
                $.post(basePath + '/resource/delete', {
                    id : id
                }, function(result) {
                    if (result.code === 1) {
                        parent.$.messager.alert('提示', result.msg, 'info');
                        resourceDataGrid.treegrid('reload');
                    }
                    progressClose();
                }, 'JSON');
            }
        });
    }
</script>
